package com.liusu.spider.wuzhou;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class Night {

    /**
     * AES加密
     */
    @Test
    public String AES_Encryption(String content,String k){


        /**
         * 加密算法
         */
        String transforms = "AES/CBC/PKCS5Padding";
        /**
         *
         */
        try {
            Cipher cipher = Cipher.getInstance(transforms);
            /**
             * 加密对象
             * [1]密钥的字节
             * [2]加密类型
             */
            SecretKeySpec spec = new SecretKeySpec(k.getBytes(),"AES");
            /**
             * [1]加密/解密
             * [2]加密规则
             */
            IvParameterSpec iv = new IvParameterSpec(k.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE,spec,iv);
            byte[] bytes = cipher.doFinal(content.getBytes());
            /*for (byte b : bytes) {
                System.out.println(b);
            }*/
            String mw = Base64.encode(bytes);
            System.out.println(mw);
            return mw;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }
        return null;
    }
    /*
     * */
    @Test
    public void context1(){
        int size = 101;
        File f = new File("D:\\k.txt");
        byte[] ks = null;
        try {
            ks= FileUtils.readFileToByteArray(f);

        } catch (IOException e) {
            e.printStackTrace();
        }

        for (int i = 1; i <= size; i++) {

            File f1 = new File("D:\\video\\"+i+".ts");
            try {
                byte[] bytes = FileUtils.readFileToByteArray(f1);

                byte[] dest_bytes = this.AES_Decrypt(bytes, ks);
                File f2 = new File("D:\\video\\temp\\"+i+"_.ts");

                FileUtils.writeByteArrayToFile(f2,dest_bytes);

                System.out.println("第{i}文件已解密".replace("{i}",i+""));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        this.mergeTs(size);
    }
    /**
     * AES解密
     * @param content
     * @param k
     */
    public byte[] AES_Decrypt(byte[] content,byte[] k){


        /**
         * 解密算法
         */
        String transforms = "AES/CBC/PKCS5Padding";
        /**
         *
         */
        try {
            Cipher cipher = Cipher.getInstance(transforms);
            /**
             * 解密对象
             * [1]密钥的字节
             * [2]解密类型
             */
            IvParameterSpec iv = new IvParameterSpec("0000000000000000".getBytes());
            SecretKeySpec spec = new SecretKeySpec(k,"AES");
            /**
             * [1]加密/解密
             * [2]加密规则
             */
            cipher.init(Cipher.DECRYPT_MODE,spec,iv);

            /**
             * A-Z
             * a-z
             * +/
             */
            byte[] bytes = cipher.doFinal(content);

            return bytes;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 合并下载好的ts片段
     */
    private void mergeTs(Integer size) {
        try {

            File file = new File("D:\\video\\start.mp4");

            if (file.exists()) {
                file.delete();
            }else{
                file.createNewFile();
            }
            FileOutputStream stream = new FileOutputStream(file);

            for (int i =1;i<=size;i++) {
                File f = new File("D:\\video\\temp\\"+i+"_.ts");


                stream.write(FileUtils.readFileToByteArray(f));

                System.out.println("第{i}文件已合并".replace("{i}",i+""));
            }
            stream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public  void main1() {
        File f1 = new File("D:\\video\\"+120+".ts");

        try {
            byte[] bytes = FileUtils.readFileToByteArray(f1);
            byte[] arr = new byte[bytes.length+9];
            System.arraycopy(bytes,0,arr,0,bytes.length);
            System.out.println(arr.length%16);
            File f = new File("D:\\k.txt");
            /*byte[] ks = FileUtils.readFileToByteArray(f);
            byte[] t =this.AES_Decrypt(arr,ks);
            FileUtils.writeByteArrayToFile(new File("D:\\video\\t_1_1_1.ts"),t);*/
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
